﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FootballProject.DAL.DatatAccessLib;

namespace FootballProject.BL.BusinessManagers
{
    /// <summary>
    /// Author: Ivan Shikht
    /// </summary>
    class DivisionManager
    {
        #region Fields
        private readonly EntitiesContext _context;
        #endregion

        #region Constructors
        public DivisionManager()
        {
            _context = new EntitiesContext();
        }
        #endregion

        #region Methods

        public List<Division> FindAll()
        {
            IQueryable<Division> rDivision = from division in _context.DivisionSet
                                             select division;
            return rDivision.ToList();
        }

        public List<Division> FindDivisionsByName(string divisionName)
        {
            IQueryable<Division> rDivision = from division in _context.DivisionSet
                                             where division.DivisionName.StartsWith(divisionName)
                                             select division;
            return rDivision.ToList();
        }

        public Division FindDivisionById(Guid divisionId)
        {
            IQueryable<Division> rDivision = from division in _context.DivisionSet
                                             where division.DivisionID == divisionId
                                             select division;
            return rDivision.First();
        }

        public void UpdateDivision(Division division)
        {
            _context.Attach(division);
            division.SetAllModified(_context);
            _context.SaveChanges();
        }

        public void AddDivision(Division division)
        {
            _context.AddToDivisionSet(division);
            _context.SaveChanges();
        }


        public void DeleteDivision(Division division)
        {
            _context.Attach(division);
            _context.DeleteObject(division);
            _context.SaveChanges();
        }
        #endregion
    }
}
